package ru.tpu.osu.faces.dao;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.*;

@Entity
@Table(name="osu_user", uniqueConstraints = {@UniqueConstraint(columnNames={"email"})})
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1537785824646338805L;
	@Id
	private String login;
	@Column(nullable = false)
	private String password;
	@Column(nullable = false)
	private String name;
	@Column(nullable = false)
	private String surname;
	@Column(nullable = false)
	private String patronymic;
	@Temporal(value=TemporalType.DATE)
	@Column(nullable = false)
	private Date dateOfBirth;
	@Column
	private String city;
	@Column
	private String street;
	@Column
	private String flat;
	@Column
	private String zipCode;
	@Column(nullable = false)
	private String email;
	@Column
	private String phone;
	@Column
	private String icq;
	@Column
	private String skype;
	@Column(nullable = false)
	private Boolean activated;
	@Column
	private String activationId;
	public String getActivationId() {
		return activationId;
	}
	public void setActivationId(String activationId) {
		this.activationId = activationId;
	}
	public Boolean getActivated() {
		return activated;
	}
	public void setActivated(Boolean activated) {
		this.activated = activated;
	}
	@OneToMany(mappedBy="sender")
	private List<PrivateMessage> sentMessages;
	@OneToMany(mappedBy="reciever")
	private List<PrivateMessage> recievedMessages;
	@OneToOne(mappedBy="user")
	private Student student;
	@OneToOne(mappedBy="user")
	private Teacher teacher;
	@OneToOne(mappedBy="user")
	private Administrator administrator;
	
	public Administrator getAdministrator() {
		return administrator;
	}
	public void setAdministrator(Administrator administrator) {
		this.administrator = administrator;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public List<PrivateMessage> getSentMessages() {
		return sentMessages;
	}
	public void setSentMessages(List<PrivateMessage> sentMessages) {
		this.sentMessages = sentMessages;
	}
	public List<PrivateMessage> getRecievedMessages() {
		return recievedMessages;
	}
	public void setRecievedMessages(List<PrivateMessage> recievedMessages) {
		this.recievedMessages = recievedMessages;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getPatronymic() {
		return patronymic;
	}
	public void setPatronymic(String patronymic) {
		this.patronymic = patronymic;
	}
	public Date getDateOfBirth() {
		return dateOfBirth;
	}
	public void setDateOfBirth(Date dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getFlat() {
		return flat;
	}
	public void setFlat(String flat) {
		this.flat = flat;
	}
	public String getZipCode() {
		return zipCode;
	}
	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getIcq() {
		return icq;
	}
	public void setIcq(String icq) {
		this.icq = icq;
	}
	public String getSkype() {
		return skype;
	}
	public void setSkype(String skype) {
		this.skype = skype;
	}
	public Boolean getIsTeacher()
	{
		return getTeacher() != null;
	}
	public Boolean getIsStudent()
	{
		return getStudent() != null;
	}
	public Boolean getIsAdministrator()
	{
		return getAdministrator() != null;
	}
	public String getShortName() {
		return String.format("%1$s %2$.1s. %3$.1s.", getSurname(),getName(),getPatronymic()); 
	}
	public String toString() {
		return getShortName();
	}
	public boolean equals(Object other) {
        return other instanceof User  ? login == ((User) other).login : false;
    }
    public int hashCode() {
        return login.hashCode();
    }
}
